package pers.wwz.heic.demos.web;

import openize.heic.decoder.HeicImage;
import openize.heic.decoder.PixelFormat;
import openize.io.IOFileStream;
import openize.io.IOMode;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.awt.image.DataBufferInt;
import java.io.File;
import java.io.IOException;

public class Test {

    public static void main(String[] args) {
        try (IOFileStream fs = new IOFileStream("D:\\my_projects\\heic-20250814\\IMG_0385.HEIC", IOMode.READ))
        {
            HeicImage image = HeicImage.load(fs);

            int width = (int)image.getWidth();
            int height = (int)image.getHeight();

            BufferedImage outImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
            final int[] dstArray = ((DataBufferInt) outImage.getRaster()
                    .getDataBuffer()).getData();
            int[] pixels = image.getInt32Array(PixelFormat.Argb32, dstArray);

            if (pixels != dstArray)
            {
                outImage.setRGB(0, 0, width/10, height/10, pixels, 0, width);
            }
            ImageIO.write(outImage, "PNG", new File("output.png"));
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
}
